<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option v-for="item in cardStatusList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button type=" primary" class="search-btn" @click="doSearch()">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>
      <el-button @click="delCartList">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" prop="id" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="cardStatusDays" />
        <el-table-column label="操作" fixed="right" width="180">
          <template v-slot="scope">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="editCard(scope.row.id)">编辑</el-button>
            <el-popconfirm title="确认删除该行数据吗？" @onConfirm="deleteCard(scope.row.id) ">
              <el-button slot="reference" size="mini" type="text" style="margin-left: 10px;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { getMonthCardApi, delCardAPI, delAllCardAPI } from '@/api/user'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        carNumber: '', // 车辆号码
        personName: '', // 车主姓名
        cardStatus: '' // 车辆状态
      },
      list: [],
      total: 0,
      cardStatusList: [
        {
          id: 0,
          name: '有效'
        },
        {
          id: 1,
          name: '已过期'
        },
        {
          id: null,
          name: '全部'
        }
      ],
      selectedList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async  getList() {
      // 获取列表
      const res = await getMonthCardApi(this.params)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 状态判断
    cardStatusDays(form) {
      if (form.cardStatus === 0) {
        return '有效'
      } else {
        return '已过期'
      }
    },
    // 点击到分页
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getList()
    },
    // 点击搜索
    doSearch() {
      // 更新请求参数
      this.params.page = 1 // 重置页码为第一页
      this.params.carNumber = this.params.carNumber.trim() // 清除车牌号码前后的空格
      this.params.personName = this.params.personName.trim() // 清除车主姓名前后的空格
      this.params.cardStatus// 状态保持不变
      // 清空搜索框
      // this.params.carNumber = ''
      // this.params.personName = ''
      // this.params.cardStatus = ''
      // 重新获取数据
      this.getList()
    },
    // 点击编辑
    editCard(id) {
      this.$router.push({
        path: '/cardAdd',
        query: {
          id
        }
      })
    },
    // 点击删除
    async  deleteCard(id) {
      await delCardAPI(id)
      if (this.list.lenght === 1 && this.params.page > 1) { this.params.page-- }
      this.getList()
      this.$message.success('删除成功')
    },
    // 多选
    handleSelectionChange(rowList) {
      console.log(rowList)
      this.selectedList = rowList
    },
    // 批量删除
    delCartList() {
      this.$confirm('此操作将永久删除选择的月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 未选择删除的 id，给提示
        if (this.selectedList.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择要删除的月卡!'
          })
        } else {
          // 处理id
          await delAllCardAPI(this.selectedList.map(item => item.id))
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch((error) => {
        console.log(error)
      })
    }
  }
}

</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
